package eic.muckup.garden;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Eic_mock_upActivity extends Activity {
	
	//Activity start on result
	private final int mMeasureings = 0; 
	private final int mLogIn = 1;
	private final int mLimitSetup = 2;
	
	private final int mMoisture = 0;
	private final int mTemperature = 1;
	
	private final String MIN_MOISTURE_VALUE = "minMoisture";
	private final String MAX_MOISTURE_VALUE = "maxMoisture";
	private final String MIN_TEMPERATURE_VALUE = "minTemperature";
	private final String MAX_TEMPERATURE_VALUE = "maxTemperature";
	
	
	private final String loginDone = "LoginPreference";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status_view);
    }
    
    public void MoistureData(View view){
    	Intent intent = new Intent();
		intent.setClass(this, Measurings.class);
		intent.putExtra("DataType", mMoisture);
		
		startActivityForResult(intent, mMeasureings);
    }
    
    
    public void TemperatureData(View view){
    	Intent intent = new Intent();
		intent.setClass(this, Measurings.class);
		intent.putExtra("DataType", mTemperature);
		
		startActivityForResult(intent, mMeasureings);
    }
    
    @Override
    protected void onResume()
    {
    	SharedPreferences setting = getSharedPreferences(loginDone, Activity.MODE_PRIVATE );
    	super.onResume();
    	if(setting.getBoolean("TempLogin", false )){
    		setContentView(R.layout.status_view);
    	}
    	else
    	{
    		Intent intent = new Intent();
    		intent.setClass(this, LoginActivity.class);
    		startActivityForResult(intent, mLogIn);
    	}
    }
    
    public boolean onCreateOptionsMenu(Menu menu) 
	{
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.status_menu_view, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	    // Handle item selection
	    switch (item.getItemId()) {
	    		
	        case R.id.LimitSetup:
	        	Intent intent = new Intent();
	    		intent.setClass(this, LimitSetupActivity.class);
	    		startActivityForResult(intent, mLimitSetup);
	            return true;
	        case R.id.Watering:
	        	Intent intentToWateringAc = new Intent();
	        	intentToWateringAc.setClass(this,WateringActivity.class);
	        	startActivity(intentToWateringAc);
	        	
	            return true;
	        case R.id.Logout:
	        	SharedPreferences setting = getSharedPreferences(loginDone, Activity.MODE_PRIVATE );
	        	SharedPreferences.Editor edit = setting.edit();
	        	edit.putBoolean("TempLogin", false);
	        	edit.commit();
	        	
	        	Intent myIntent = new Intent();
	        	myIntent.setClass(this, LoginActivity.class);
	        	startActivityForResult(myIntent, mLogIn);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_CANCELED && requestCode == mLogIn){
			finish();
			System.exit(0);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public void calculate(View view){
		SharedPreferences pref = getSharedPreferences("LIMIT_VALUE", Activity.MODE_PRIVATE);
    	
		int tempStatus = 0;
		int moistureStatus = 0;
    	EditText valueEditText = (EditText) findViewById(R.id.TemperatureValueEditText);
    	ImageView statusImage = (ImageView)findViewById(R.id.ResultImage);
    	if(valueEditText.getText() != null){
    		ImageView image = (ImageView)findViewById(R.id.TemperatureImage);
    		
	    	Float value = Float.parseFloat(valueEditText.getText().toString().replace(',', '.'));
	    	if(value < pref.getFloat(MIN_TEMPERATURE_VALUE, -20)){
	    		image.setImageResource(R.drawable.ice_flower);
	    		tempStatus = -1;
	    	}
	    	else if(value > pref.getFloat(MAX_TEMPERATURE_VALUE, 100)){
	    		image.setImageResource(R.drawable.fire_flower);
	    		tempStatus = 1;
	    	}
	    	else{
	    		image.setImageResource(R.drawable.good_flower);
	    	}
    	}
    	
    	EditText moistureEditText = (EditText) findViewById(R.id.MoisturValueEditText);
    	if(moistureEditText.getText() != null){
    		ImageView image = (ImageView)findViewById(R.id.MoistorImage);
    		Float value = Float.parseFloat(moistureEditText.getText().toString().replace(',', '.'));
	    	if(value < pref.getFloat(MIN_MOISTURE_VALUE, -20)){
	    		image.setImageResource(R.drawable.thirsty_smily);
	    		moistureStatus = -1;
	    	}
	    	else if(value > pref.getFloat(MAX_MOISTURE_VALUE, 100)){
	    		image.setImageResource(R.drawable.flooded_flowe);
	    		moistureStatus = 1;
	    	}
	    	else{
	    		image.setImageResource(R.drawable.flower);
	    	}
    	}
    	
    	if(tempStatus == -1 && moistureStatus == 1 ){
    		statusImage.setImageResource(R.drawable.flooded_frozen);
    	}
    	else if(tempStatus == 1 && moistureStatus == -1){
    		statusImage.setImageResource(R.drawable.dead_flower);
    	}
    	else{
    		statusImage.setImageResource(R.drawable.normal_flower);
    	}
    	
	}
    
}